package com.yanu.trace.t2s.controller;

import com.yanu.trace.t2s.entity.Dept;
import com.yanu.trace.t2s.service.DeptService;
import com.yanu.trace.t2s.util.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

//http://127.0.0.1:8080/dept/get/10

//22222222222222222222
@CrossOrigin
@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    DeptService deptService;

    @PostMapping("/fu")
    @ResponseBody
    public Message upload(@RequestParam("file") MultipartFile file) {
        Message msg = new Message(200,"文件上传成功！",null);
        if (file.isEmpty()) {
            msg.setCode(502);
            msg.setMsg("请选择文件");
            return  msg;
        }
        String fileName = file.getOriginalFilename();
        String filePath = "d:/";
        File dest = new File(filePath + fileName);
        try {
            file.transferTo(dest);
            System.out.println("ok");

            return  msg;
        } catch (IOException e) {

        }
        msg.setCode(503);
        msg.setMsg("系统原因请重试");
        return  msg ;
    }





    @GetMapping("get/{deptno}")
    public Dept fn1(@PathVariable int deptno){

        return  deptService.findByDeptno(deptno);
    }

    @GetMapping("get")
    public List<Dept> fd2(){

        return  deptService.findAll();
    }
    @GetMapping("get1")
    public Message fd3( Dept dept ){
        System.out.println("DeptController.fd3");
        System.out.println(dept);
        Message message = new Message(501,"部门名称过长", "部门名称过长");
        return   message; //  null;// deptService.findAll();
    }

    @GetMapping("get2")
    public List<Dept> fd4( Dept dept ){
        System.out.println("DeptController.fd3");
        System.out.println(dept);
        return null;// deptService.findAll();
    }

}
